package com.netty.rpc.encode;

import com.alibaba.fastjson.JSONObject;
import io.netty.buffer.ByteBuf;


public class CodeC {

    public static ByteBuf  encode(Object object, ByteBuf byteBuf){
        byte[] bytes = JSONObject.toJSONBytes(object);
        byteBuf.writeInt(bytes.length);
        byteBuf.writeBytes(bytes);
        return byteBuf;
    }

    public static  <T> T decode(Class clazz, ByteBuf byteBuf){
        int length  = byteBuf.readInt();
        byte[] reqBytes = new byte[length];
        byteBuf.readBytes(reqBytes);
        return JSONObject.parseObject(reqBytes, clazz);
    }

}
